<?php
require_once('ActionsController.php');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ActionLawsController extends ActionsController {
    
    const PROPAGATION_GROUP = 'group';
    const PROPAGATION_CITY = 'city';
    const PROPAGATION_DEPARTMENT = 'department';
    const PROPAGATION_STATE = 'state';
    const PROPAGATION_COUNTRY = 'counntry';
    public function beforeFilter(){
        $group = $this->getCurrentUserGroup();
        $this->set('data2', $group);
}
    public $layout = 'group';
    public $uses = array('ActionLaw','Action', 'User','ActionLawSupr','ActionLawUserChoice','Place');
    
    
    public function view() {
        
        $idG = $this->getCurrentUserGroupId();
        $dateAct = date("Y-m-d H:i:s", time());
        //$listActionLaw = Liste des actions du groupe de l'utilisateur
        $listActionLawOn = $this->ActionLaw->find('all', array(
            'conditions' => array(
                'Action.groups_id' => $idG,
                'dateOn !=' => NULL)
            ));
         $listActionLawOff = $this->ActionLaw->find('all', array(
            'conditions' => array(
                'Action.groups_id' => $idG,
                'dateOn' => NULL
            )           
        ));

        foreach($listActionLawOff as $action){
                $lawSupr = $this->ActionLawSupr->find('first', array(
                    'conditions' => array(
                        'action_law_id' => $action['ActionLaw']['id']
                    )
                ));
                if ($lawSupr['ActionLawSupr']['dateFin'] < $dateAct && empty($lawSupr['ActionLaw']['dateOn'])) {
                                  
                  $this->ActionLawUserChoice->deleteAll(array(
                    'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id']
                  ));
                  $this->ActionLawSupr->deleteAll(array(
                      'action_law_id' => $action['ActionLaw']['id']
                  ));
                  $this->ActionLaw->deleteAll(array(
                      'ActionLaw.id' => $action['ActionLaw']['id']
                  ));
                  $this->Action->deleteAll(array(
                      'Action.id' => $action['Action']['id']
                  ));
         }
        }
       $listActionLawOff = $this->ActionLaw->find('all', array(
                    'conditions' => array(
                    'Action.groups_id' => $idG,
                    'dateOn' => NULL
            )));
        $this->set('listLawOn', $listActionLawOn);
        $this->set('listLawOff', $listActionLawOff);

    }
  public function abolir ($idLaw){
      //$Law = Objet Loi (pour celle sélectionnée : $idLaw) 
      $law = $this->ActionLaw->find('first', array(
          'conditions' => array(
              'ActionLaw.id' => $idLaw
          )
      ));
      
      $action = $this->Action->find('first', array(
          'conditions' => array(
              'Action.id' => $law['ActionLaw']['actions_id']
          )
      )   );
      //$user = Objet Utilisateur (ayant proposé cette loi)
      $user = $this->User->find('first', array(
          'conditions' => array(
              'User.id' => $law['Action']['users_id']
          )
      ));
      //$lawSupr = Objet ActionLawSupr ( pour la loi)
      $lawSupr = $this->ActionLawSupr->find('first',array(
          'conditions' => array(
              'ActionLawSupr.action_law_id' => $law['ActionLaw']['id']
          )
      ));
      
      //$voteUser = Recupere le vote de l'utilisateur si il a déja voté pour la suppression de la loi
      $currentUser = $this->getCurrentUser();
      $voteUser = $this->ActionLawUserChoice->find('first', array(
           'conditions'=> array(
                'ActionLawUserChoice.users_id' => $currentUser['User']['id'],
                'ActionLawUserChoice.actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'])
            )
      );
        
      $this->set('law',$law);
      $this->set('lawSupr',$lawSupr);
      $this->set('action',$action);
      $this->set('user',$user);
      $this->set('voteUser',$voteUser);
      $this->set('userGroup', $this->getCurrentUserGroupId());
  
    }
    public function createLaw(){
        
        $TEMPS_SONDAGE_LAW_ACCEPTER_SECONDE = 15*(24*3600);// durée du sondage avant accepter loi selon la loi formelle définie pour le groupe
        $form = $this->request->data;
        if (!empty($form)){
        $dateAct = date("Y-m-d H:i:s", time());
        $dateFin =  date("Y-m-d H:i:s", time()+ $TEMPS_SONDAGE_LAW_ACCEPTER_SECONDE);
        $action = $this->addAction($form['ActionLaw']['niveauPropagation']);
        debug($action);
        $actionLaw = $this->ActionLaw->save(array(
               'title'=> $form['ActionLaw']['title'],
               'description' => $form['description'],
               'nbSup' => 0,
               'nbCon' =>0,
               'actions_id' => $action['Action']['id'],
               'dateOn' => NULL
            ));

           $actionLawSupr = $this->ActionLawSupr->save(array(
               'action_law_id' => $actionLaw['ActionLaw']['id'],
               'dateDebut' => $dateAct,
               'dateFin' => $dateFin
           ));
             $this->Session->setFlash('Loi ajoutée.');
            $this->redirect('./view');
       }
        $this->set('form',$form);
    }
    
    public function lawSupprimer($idLaw, $voteSupr, $voteAccep){
      $TEMPS_SONDAGE_LAW_SECONDE = 15*(24*3600); //Durée du sondage fixé a 15 jours.
      $currentUser = $this->getCurrentUser();
      $NB_MIN_SUP = 10;
      $dateAct = date("Y-m-d H:i:s", time());

      $law = $this->ActionLaw->find('first', array(
          'conditions' => array(
              'ActionLaw.id' => $idLaw
          )
      ));
         
            
      $action = $this->Action->find('first', array(
          'conditions' => array(
              'Action.id' => $law['ActionLaw']['actions_id']
          )
      )   );
      
      $lawSupr = $this->ActionLawSupr->find('first',array(
          'conditions' => array(
              'ActionLawSupr.action_law_id' => $law['ActionLaw']['id']
          )
      ));
        //Si l'utilisateur vote : supprimer la loi
        if ($voteSupr == 1){
        $save = $this->ActionLaw->save(array(
            'id'=> $idLaw,        
            'nbSup' => $law['ActionLaw']['nbSup'] + 1
          ));
            $this->ActionLawUserChoice->save(array(
            'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'],
            'vote' => 1,
            'users_id' => $currentUser['User']['id']
        ));
        }
        //Sinon si il vote pour la conserver
        else if($voteSupr == 0){

            $save =$this->ActionLaw->save(array(
                'id'=> $idLaw,        
                'nbCon' => $law['ActionLaw']['nbCon'] + 1
            ));
            $this->ActionLawUserChoice->save(array(
            'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'],
            'vote' => 0,
            'users_id' => $currentUser['User']['id']
        ));
        }
            

        
        $law = $this->ActionLaw->find('first', array(
          'conditions' => array(
              'ActionLaw.id' => $idLaw
          )));
        
        if($voteAccep == 0){
            if ($law['ActionLaw']['nbSup'] == 5){
            $dateAct = date("Y-m-d H:i:s", time());
            $dateFin = date("Y-m-d H:i:s", time() + ($TEMPS_SONDAGE_LAW_SECONDE)); 

                $this->ActionLawSupr->save(array(
                    'id' => $lawSupr['ActionLawSupr']['id'],
                    'dateDebut' => $dateAct,
                    'dateFin' => $dateFin
                ));
            }
        
        if ($law['ActionLaw']['nbSup'] >= $NB_MIN_SUP){                      
                      $this->ActionLawUserChoice->deleteAll(array(
                        'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id']
                      ));
                      $this->ActionLawSupr->deleteAll(array(
                          'action_law_id' => $action['ActionLaw']['id']
                      ));
                      $this->ActionLaw->deleteAll(array(
                          'ActionLaw.id' => $action['ActionLaw']['id']
                      ));
                      $this->Action->deleteAll(array(
                          'Action.id' => $action['Action']['id']
                      ));
               $this->Session->setFlash('La loi a été supprimé.');
            $this->redirect('./view');                   
        }else if ($lawSupr['ActionLawSupr']['dateFin'] < $dateAct){
                       
                $this->ActionLaw->save(array(
                'ActionLaw.id'=> $idLaw,
                'nbCon' => 0));
            $this->Session->setFlash('Le référendum est terminé.');
            $this->redirect($this->referer());      
            }else{
            $this->Session->setFlash('Vote pris en compte.');
            $this->redirect($this->referer());
            }
        
    }
    }
    
public function lawAnnulerVote($idLaw,$annulerVoteAccep){
    $currentUser = $this->getCurrentUser();
    $law = $this->ActionLaw->find('first', array(
          'conditions' => array(
              'ActionLaw.id' => $idLaw
          )
    ));
         
   $lawSupr = $this->ActionLawSupr->find('first',array(
          'conditions' => array(
              'ActionLawSupr.action_law_id' => $idLaw
          )
   ));
  $choice = $this->ActionLawUserChoice->find('first',array(
       'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'],
       'users_id' => $currentUser['User']['id'])
       );
   if ($choice['ActionLawUserChoice']['vote'] == 1 ){
       
           $nbSup = $law['ActionLaw']['nbSup'];
           $newNbSup = $nbSup - 1;
           $this->ActionLaw->save(array(
            'id'=> $idLaw,        
            'nbSup' => $newNbSup
          ));
   }else if ($choice['ActionLawUserChoice']['vote'] == 0 ){
       echo($choice['ActionLawUserChoice']['vote']);
            $nbCon = $law['ActionLaw']['nbCon'];
            $newNbCon = $nbCon - 1;
           $this->ActionLaw->save(array(
            'id'=> $idLaw,
            'nbCon' => $newNbCon
          ));
    }
       $this->ActionLawUserChoice->deleteAll(array(
       'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'],
       'users_id' => $currentUser['User']['id'])
       );
     $this->Session->setFlash('Vote pris en compte.');
     $this->redirect($this->referer());
        
  }
  public function lawAccepter($idLaw){
     $law = $this->ActionLaw->find('first', array(
          'conditions' => array(
              'ActionLaw.id' => $idLaw
          )
      ));
      $lawSupr = $this->ActionLawSupr->find('first',array(
          'conditions' => array(
              'ActionLawSupr.action_law_id' => $idLaw
          )));
      $action = $this->Action->find('first', array(
          'conditions' => array(
              'Action.id' => $law['ActionLaw']['actions_id']
          )
      ));
      //$user = Objet Utilisateur (ayant proposé cette loi)
      $user = $this->User->find('first', array(
          'conditions' => array(
              'User.id' => $law['Action']['users_id']
          )
      ));
      
      $currentUser = $this->getCurrentUser();
      $voteUser = $this->ActionLawUserChoice->find('first', array(
           'conditions'=> array(
                'ActionLawUserChoice.users_id' => $currentUser['User']['id'],
                'ActionLawUserChoice.actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'])
            )
      );
      
      $this->set('law',$law);
      $this->set('currentUser',$currentUser);
      $this->set('lawSupr',$lawSupr);
      $this->set('action',$action);
      $this->set('user',$user);
      $this->set('userGroup', $this->getCurrentUserGroupId());
      $this->set('voteUser',$voteUser);

  }
  
  public function lawAccepterVote($idLaw){
      $NB_MIN_ACCEPTER =5;
      $this->lawSupprimer($idLaw, 1, 1);
     
   $lawSupr = $this->ActionLawSupr->find('first',array(
          'conditions' => array(
              'ActionLawSupr.action_law_id' => $idLaw
          )
   ));
   
      if ($lawSupr['ActionLaw']['nbSup']== $NB_MIN_ACCEPTER){
          $dateAct = date("Y-m-d H:i:s", time());

          $this->ActionLaw->save(array(
              'id' => $idLaw,
              'dateOn' => $dateAct,
              'nbSup' => 0
          ));
          $this->ActionLawSupr->save(array(
              'id' => $lawSupr['ActionLawSupr']['id'],
              'dateDebut'=> NULL,
              'dateFin' => NULL
          ));
        $this->ActionLawUserChoice->deleteAll(array(
       'actions_laws_suprs_id' => $lawSupr['ActionLawSupr']['id'])
       );
        $this->Session->setFlash('La loi a était acceptée par la communauté.');
            $this->redirect('view');

      }else {
          $this->Session->setFlash('Vote pris en compte.');
          $this->redirect($this->referer());
      }
  }
}
?>
